package com.saasovation.identityaccess.resource;

import com.saasovation.common.media.OvationsMediaType;
import com.saasovation.identityaccess.application.command.AssignUserToRoleCommand;
import com.saasovation.identityaccess.application.command.ProvisionRoleCommand;
import com.saasovation.identityaccess.domain.model.access.Role;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/tenants/{tenantId}/roles")
public class RoleResource extends AbstractResource {

    public RoleResource() {
        super();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({OvationsMediaType.ID_OVATION_TYPE})
    public Response provisionRole(ProvisionRoleCommand command) {
        Role role = this.accessApplicationService().provisionRole(command);
        return Response.ok(role.name()).build();
    }

    @POST
    @Path("{roleName}/users")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({OvationsMediaType.ID_OVATION_TYPE})
    public Response assignUserToRole(@PathParam("tenantId") String tenantId,
                                     @PathParam("roleName") String roleName,
                                     AssignUserToRoleCommand command) {
        command.setTenantId(tenantId);
        command.setRoleName(roleName);
        this.accessApplicationService().assignUserToRole(command);
        return Response.ok().build();
    }
}
